package com.jrx.anytxn.investor.job.file.loan.order.config;

import com.jrx.anytxn.investor.config.AnytxnBatchProperties;
import com.jrx.anytxn.investor.config.InvestorFileConfig;
import com.jrx.anytxn.investor.entity.AmLoanMainInvestorInfo;
import com.jrx.anytxn.investor.job.file.day.order.step.FileListener;
import com.jrx.anytxn.investor.job.file.day.order.step.FileWriter;
import com.jrx.anytxn.investor.job.file.loan.order.step.LoanOrderProcessor;
import com.jrx.anytxn.investor.job.file.loan.order.step.LoanOrderReader;
import com.jrx.anytxn.investor.service.IInvestorFileService;
import com.jrx.anytxn.param.dto.investor.FileBean;
import com.jrx.anytxn.param.entity.PrInvestorTable;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorTableMapper;
import com.jrx.anytxn.param.service.investor.IInvestorParamFileService;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: 放款(借据)明细文件
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-06-08 16:40
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class InvestorLoanOrderConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private LoanOrderProcessor loanOrderProcessor;
    @Autowired
    private IInvestorParamFileService fileService;
    @Autowired
    private InvestorFileConfig fileConfig;
    @Resource
    private ExtPrInvestorTableMapper extPrInvestorTableMapper;

    @Bean
    public Job loanOrderJob() {
        return jobBuilderFactory.get("loanOrderJob")
                .start(loanOrderStep())
                .listener(loanOrderListener())
                .build();
    }

    @Bean
    public Step loanOrderStep() {
        return stepBuilderFactory.get("loanOrderStep")
                .<AmLoanMainInvestorInfo, FileBean>chunk(anytxnBatchProperties.getFileConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(loanOrderReader())
                .processor(loanOrderProcessor)
                .writer(loanOrderWriter())
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getFileConfig().getThrottleLimit())//多线程
                .build();
    }


    @Bean
    @StepScope
    public MyBatisPagingItemReader<AmLoanMainInvestorInfo> loanOrderReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new LoanOrderReader(sqlSessionFactory, anytxnBatchProperties.getFileConfig().getPageSize(),org);
    }

    @Bean
    @StepScope
    public ItemWriter<FileBean> loanOrderWriter() {
        return new FileWriter(fileService, fileConfig, TransBizConstant.LOAN_ORDER_FILE_NAME);
    }

    @Bean
    public JobExecutionListener loanOrderListener() {
        List<PrInvestorTable> investorList = extPrInvestorTableMapper.selectAllByTableStatus(TransBizConstant.STATUS_1);
        return new FileListener(organizationTableService,fileConfig, investorList,TransBizConstant.LOAN_ORDER_FILE_NAME);    }
}
